/**
 * $Id$
 * Copyright 2011-2014 Oak Pacific Interactive. All rights reserved.
 */
package com.malphi.framework.memcache.client;

import java.util.Date;
import java.util.Map;

import com.danga.MemCached.MemCachedClient;
import com.malphi.framework.core.xmlconfig.impl.MemCacheConfig;
import com.malphi.framework.memcache.manage.MemCacheManager;

/**
 * @author <a href="mailto:ruofei.ma@renren-inc.com">ma ruofei</a>
 * @version 1.0 2011-11-21 上午11:21:55
 * @since 1.0
 */
public class CacheClient {
    
    public static final long SECOND = 1L;
    public static final long MINUTE = 60 * SECOND;
    public static final long HOUR = 60 * MINUTE;
    public static final long DAY = 24 * HOUR;
    public static final long WEEK = 7 * DAY;
    public static final long MONTH = 30 * DAY;
    public static final long YEAR = 365 * DAY;

    private static MemCachedClient client = MemCacheManager.getCacheClient();
    private static MemCacheConfig config = MemCacheManager.getConfig();

    public static boolean set(String key, Object value) {
        return client.set(getRealKey(key), value);
    }

    public static boolean set(String key, Object value, long second) {
        return client.set(getRealKey(key), value, new Date(System.currentTimeMillis() + second * 1000));
    }

    public static Object get(String key) {
        return client.get(getRealKey(key));
    }

    public static Map<String, Object> getMulti(String[] keys) {
        for (int i = 0; i < keys.length; i++) {
            keys[i] = getRealKey(keys[i]);
        }
        return client.getMulti(keys);
    }

    public static boolean delete(String key) {
        return client.delete(getRealKey(key));
    }

    public static boolean keyExists(String key) {
        return client.keyExists(getRealKey(key));
    }

    public static Object[] getMultiArray(String[] keys) {
        for (int i = 0; i < keys.length; i++) {
            keys[i] = getRealKey(keys[i]);
        }
        return client.getMultiArray(keys);
    }
    
    public static String getRealKey(String key) {
        return config.getPrefix() + key;
    }
}
